package com.quanyan.user.web;

import com.quanyan.common.user.AppUserCacheInfo;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2017/3/3.
 */
@Controller
public class BaseController {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String userCookiesNames = "userCookiesName";

    /**
     * 获取uid
     * @param request
     * @return
     */
    public Integer getUidByUserCookiesName(HttpServletRequest request) throws UnsupportedEncodingException {
        Integer uid = null;
        String userCookiesName = request.getHeader(userCookiesNames);
        RedisKey redisKey = new RedisKey(userCookiesName, "");
        AppUserCacheInfo appUserCacheInfo = (AppUserCacheInfo)redisTemplate.getObjectFromC(redisKey, AppUserCacheInfo.class);
        if (appUserCacheInfo != null)
            uid = appUserCacheInfo.getUser_id();
        return uid;
    }


}
